package luj.game.robot.internal.concurrent.parent.spawn;

import akka.actor.ActorRef;
import java.util.HashMap;
import java.util.LinkedList;
import luj.game.robot.internal.concurrent.instance.RobotInstanceActor;
import luj.game.robot.internal.concurrent.parent.RobotParentActor;
import luj.game.robot.internal.start.botinstance.RobotState;
import luj.spring.anno.Internal;

@Internal
final class OnRobotSpawn implements RobotParentActor.Handler<RobotSpawnMsg> {

  /**
   * @see luj.game.robot.internal.concurrent.instance.prestart.OnInstancePrestart#onHandle
   */
  @Override
  public void onHandle(Context ctx) {
    RobotParentActor self = ctx.getActorState(this);

    RobotInstanceActor workerActor = createInstance(ctx);
    ActorRef workerRef = ctx.createActor(workerActor);
    self.getRobotList().add(workerRef);

    updateAdmin(self, workerActor);
  }

  private RobotInstanceActor createInstance(Context ctx) {
    RobotParentActor self = ctx.getActorState(this);
    RobotSpawnMsg msg = ctx.getMessage(this);

    var robotState = new RobotState();
    robotState.setDataMap(new HashMap<>());
    robotState.setReceiveHistory(new LinkedList<>());

    robotState.setStatus(msg.getInitStatus());
    robotState.setStatusMap(msg.getStatusMap());

//    robotState.setCurAction(msg.getAction());
    robotState.setStepIndex(-1);

    return new RobotInstanceActor(robotState, self.getWorkerDependency(),
        msg.getTickMsMin(), msg.getTickMsMax(), ctx.getActorRef(), self.getBossDependency(), self.getAdminRef());
  }

  @Deprecated
  private void updateAdmin(RobotParentActor self, RobotInstanceActor instance) {
//    self.getAdminRef().tell(_lujbean.createBean(UpdateInstanceMsg.class, (b, m) -> b
//        .set(m::index, instance.getIndex())
//    ).getInstance());
  }
}
